#ifndef __HardwarePixelBuffer__
#define __HardwarePixelBuffer__


#include "U2PreRequest.h"
#include "U2SharedPtr.h"
#include "U2HardwareBuffer.h"
#include "U2PixelUtil.h"
#include "U2Image.h"


U2EG_NAMESPACE_BEGIN


class U2HardwarePixelBufferSharedPtr;


/** Specialisation of U2HardwareBuffer for a pixel buffer. The
	HardwarePixelbuffer abstracts an 1D, 2D or 3D quantity of pixels
	stored by the rendering API. The buffer can be located on the card
	or in main memory depending on its usage. One mipmap level of a
	texture is an example of a HardwarePixelBuffer.
*/
class _U2Share U2HardwarePixelBuffer : public U2HardwareBuffer
{
protected: 
    // Extents
    size_t mWidth, mHeight, mDepth;
    // Pitches (offsets between rows and slices)
    size_t mRowPitch, mSlicePitch;
    // Internal format
    PixelFormat mFormat;
    // Currently locked region (local coords)
    U2PixelBox mCurrentLock;
	// The current locked box of this surface (entire surface coords)
	U2Image::Box mLockedBox;

    
    /// Internal implementation of lock(), must be overridden in subclasses
    virtual U2PixelBox lockImpl(const U2Image::Box lockBox,  LockOptions options) = 0;

    /// Internal implementation of lock(), do not OVERRIDE or CALL this
    /// for HardwarePixelBuffer implementations, but override the previous method
    virtual void* lockImpl(size_t offset, size_t length, LockOptions options);

    /// Internal implementation of unlock(), must be overridden in subclasses
    // virtual void unlockImpl(void) = 0;

	/** Notify TextureBuffer of destruction of render target.
		Called by RenderTexture when destroyed.
	*/
	virtual void _clearSliceRTT(size_t zoffset);
	friend class U2RenderTexture;

public:
    /// Should be called by HardwareBufferManager
    U2HardwarePixelBuffer(size_t mWidth, size_t mHeight, size_t mDepth,
            PixelFormat mFormat,
            U2HardwareBuffer::Usage usage, bool useSystemMemory, bool useShadowBuffer);
    ~U2HardwarePixelBuffer();

    /** make every lock method from U2HardwareBuffer available.
    See http://www.research.att.com/~bs/bs_faq2.html#overloadderived
    */
    using U2HardwareBuffer::lock;	

    /** Lock the buffer for (potentially) reading / writing.
	    @param lockBox Region of the buffer to lock
	    @param options Locking options
	    @returns U2PixelBox containing the locked region, the pitches and
	    	the pixel format
	*/
	virtual const U2PixelBox& lock(const U2Image::Box& lockBox, LockOptions options);
	/// @copydoc U2HardwareBuffer::lock
    virtual void* lock(size_t offset, size_t length, LockOptions options);

	/** Get the current locked region. This is the same value as returned
	    by lock(const U2Image::Box, LockOptions)
	    @returns U2PixelBox containing the locked region
	*/        
    const U2PixelBox& getCurrentLock();
	
	/// @copydoc U2HardwareBuffer::readData
	virtual void readData(size_t offset, size_t length, void* pDest);
	/// @copydoc U2HardwareBuffer::writeData
	virtual void writeData(size_t offset, size_t length, const void* pSource,
			bool discardWholeBuffer = false);
    
    /** Copies a box from another PixelBuffer to a region of the 
    	this PixelBuffer. 
		@param dst		Source pixel buffer
    	@param srcBox	U2Image::Box describing the source region in src
    	@param dstBox	U2Image::Box describing the destination region in this buffer
		@remarks The source and destination regions dimensions don't have to match, in which
	   	case scaling is done. This scaling is generally done using a bilinear filter in hardware,
        but it is faster to pass the source image in the right dimensions.
		@note Only call this function when both  buffers are unlocked. 
     */        
    virtual void blit(const U2HardwarePixelBufferSharedPtr &src, const U2Image::Box &srcBox, const U2Image::Box &dstBox);

	/** Convenience function that blits the entire source pixel buffer to this buffer. 
		If source and destination dimensions don't match, scaling is done.
		@param src		U2PixelBox containing the source pixels and format in memory
		@note Only call this function when the buffer is unlocked. 
	*/
	void blit(const U2HardwarePixelBufferSharedPtr &src); 
	
	/** Copies a region from normal memory to a region of this pixelbuffer. The source
		image can be in any pixel format supported by OGRE, and in any size. 
	   	@param src		U2PixelBox containing the source pixels and format in memory
	   	@param dstBox	U2Image::Box describing the destination region in this buffer
        @remarks The source and destination regions dimensions don't have to match, in which
        case scaling is done. This scaling is generally done using a bilinear filter in hardware,
        but it is faster to pass the source image in the right dimensions.
		@note Only call this function when the buffer is unlocked. 
	*/
	virtual void blitFromMemory(const U2PixelBox &src, const U2Image::Box &dstBox) = 0;
	
	/** Convenience function that blits a pixelbox from memory to the entire 
		buffer. The source image is scaled as needed.
		@param src		U2PixelBox containing the source pixels and format in memory
		@note Only call this function when the buffer is unlocked. 
	*/
	void blitFromMemory(const U2PixelBox &src)
	{
		blitFromMemory(src, Box(0,0,0,mWidth,mHeight,mDepth));
	}
	
	/** Copies a region of this pixelbuffer to normal memory.
	   	@param srcBox	U2Image::Box describing the source region of this buffer
	   	@param dst		U2PixelBox describing the destination pixels and format in memory
	   	@remarks The source and destination regions don't have to match, in which
	   	case scaling is done.
		@note Only call this function when the buffer is unlocked. 
	 */
	virtual void blitToMemory(const U2Image::Box &srcBox, const U2PixelBox &dst) = 0;

	/** Convience function that blits this entire buffer to a pixelbox.
		The image is scaled as needed.
		@param src		U2PixelBox containing the source pixels and format in memory
		@note Only call this function when the buffer is unlocked. 
	*/
	void blitToMemory(const U2PixelBox &dst)
	{
		blitToMemory(Box(0,0,0,mWidth,mHeight,mDepth), dst);
	}
    
    /** Get a render target for this PixelBuffer, or a slice of it. The texture this
        was acquired from must have TU_RENDERTARGET set, otherwise it is possible to
        render to it and this method will throw an ERR_RENDERSYSTEM exception.
        @param slice    Which slice
        @returns A pointer to the render target. This pointer has the lifespan of this
        PixelBuffer.
    */
    /**********************@@@@@@@@@@@@@@@@@@@@@
    virtual RenderTexture *getRenderTarget(size_t slice=0);
    */
    
    /// Gets the width of this buffer
    size_t getWidth() const { return mWidth; }
    /// Gets the height of this buffer
    size_t getHeight() const { return mHeight; }
    /// Gets the depth of this buffer
    size_t getDepth() const { return mDepth; }
    /// Gets the native pixel format of this buffer
    PixelFormat getFormat() const { return mFormat; }
};



/** Shared pointer implementation used to share pixel buffers. */
class _U2Share U2HardwarePixelBufferSharedPtr : public U2SharedPtr<U2HardwarePixelBuffer>
{
public:
    U2HardwarePixelBufferSharedPtr() : U2SharedPtr<U2HardwarePixelBuffer>() {}
    explicit U2HardwarePixelBufferSharedPtr(U2HardwarePixelBuffer* buf);
};


U2EG_NAMESPACE_END


#endif

